using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Clanta.Web.Controls;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
//using iTextSharp.text.pdf;
using Clanta.Data;
using Clanta.Core.Mail;
using Clanta.Core.Domain;
using System.Collections.Generic;

namespace Clanta.Web.User
{
    public partial class ReportView : System.Web.UI.Page
    {
        static ReportDocument reportdoc = null;
        string reportFileAbsPath = null;
        static string path;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                reportdoc = new ReportDocument();

                string reportId = Request.QueryString.Get("id");
                string uniqueFileName = String.Format("ClantaReport-{0}.rpt", reportId);
                try
                {
                    string reportsPath = this.Server.MapPath("Reports");
                    if (!System.IO.Directory.Exists(reportsPath))
                    {
                        //TextBox1.Text = "Oops! Reports directory does not exist!";
                    }
                    reportFileAbsPath = System.IO.Path.Combine(reportsPath, uniqueFileName);
                    path = System.IO.Path.Combine(reportsPath, uniqueFileName);
                    reportdoc.Load(reportFileAbsPath);
                    CrystalReportViewerClanta.ReportSource = reportdoc;
                    string id = Request.QueryString.Get("conferenceId");
                    List<ListObject> speakers = new GeneralStatisticsInformation(Server.MapPath(@"~/Config/ClantaNHibernate.config")).GetSpeakersByConferenceId(long.Parse(Request.QueryString.Get("conferenceId")));
                    speakers.RemoveAt(0);
                    DropDownListSpeakersForMail.DataSource = speakers;
                    DropDownListSpeakersForMail.DataTextField = "Text";
                    DropDownListSpeakersForMail.DataValueField = "MailAdress";
                    DropDownListSpeakersForMail.AppendDataBoundItems = false;
                    DropDownListSpeakersForMail.DataBind();
                }
                catch
                { }
            }
        }

        protected void ButtonExportWord_Click(object sender, EventArgs e)
        {
            MemoryStream oStream; // using System.IO

            oStream = (MemoryStream)

            reportdoc.ExportToStream(

            CrystalDecisions.Shared.ExportFormatType.WordForWindows);

            Response.Clear();

            Response.Buffer = true;

            Response.ContentType = "application/msword";

            Response.BinaryWrite(oStream.ToArray());

            Response.End();

        }

        protected void ButtonSendMail_Click(object sender, EventArgs e)
        {
            OutlookEmail  mail = new OutlookEmail(DropDownListSpeakersForMail.SelectedValue, DropDownListSpeakersForMail.SelectedItem.ToString());

                //System.Diagnostics.Process.Start("mailto:chris@enovi.com")

            reportdoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, path.Replace("rpt", "pdf"));
            mail.AddAttachment(path.Replace("rpt", "pdf"), "Report");

            mail.AabenMailIOutlook();

            ReturnToStatPage();

        }

        protected void ButtonExportPdf_Click(object sender, EventArgs e)
        {
            try
            {
                MemoryStream oStream; // using System.IO

                oStream = (MemoryStream)

                reportdoc.ExportToStream(

                CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

                Response.Clear();

                Response.Buffer = true;

                Response.ContentType = "application/pdf";

                Response.BinaryWrite(oStream.ToArray());

                Response.End();
            }
            catch
            { 
            
            }
        }

        private void ReturnToStatPage()
        {
            Response.Redirect(string.Format("Default.aspx?conferenceId={0}", Request.QueryString.Get("conferenceId")));        
        }

        protected void ButtonNewStat_Click(object sender, EventArgs e)
        {
            ReturnToStatPage();
        }
    }
}
